package test2.shashindra;

import camera.*;
import UserInterface.FinalGUI;
import static com.googlecode.javacv.cpp.opencv_core.cvScalar;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_MEDIAN;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvSmooth;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.VideoInputFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import coordinates.MeanDetector;
import java.util.logging.Level;
import java.util.logging.Logger;
import objectExtraction.ColourDetector;
import utilities.AngleFinder;
import com.googlecode.javacv.FrameGrabber.Exception;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class ColorRedTest implements Runnable {

    final int INTERVAL = 10;// 1sec
    final int CAMERA_NUM; // Default camera for this time
    final int CAMERA_FOV;//angle ofthe camera in degrees
    private int IMAGE_NO;
    static CvScalar rgba_min = cvScalar(0, 0, 130, 0);// RED wide dabur birko
    static CvScalar rgba_max = cvScalar(80, 80, 255, 0);
    ColourDetector colDet = new ColourDetector();
    AngleFinder angFind = new AngleFinder();
    MeanDetector meanDet = new MeanDetector();
    IplImage image;
    CanvasFrame canvas = new CanvasFrame("Web Cam Live");
    CanvasFrame pathRGB = new CanvasFrame("Threshold RGB");
    CanvasFrame pathHSV = new CanvasFrame("Threshold HSV");
    private double theta;

    public ColorRedTest() {
        CAMERA_NUM = 0;
        CAMERA_FOV = 90;
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        pathRGB.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        //path.setContentPane(jp);
    }

    public ColorRedTest(int camera, int fov) {
        CAMERA_NUM = camera;
        CAMERA_FOV = fov;
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        pathRGB.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        // path.setContentPane(jp);
    }

    @Override
    public void run() {
        IMAGE_NO = FinalGUI.initializeCamera();
        FrameGrabber grabber = new VideoInputFrameGrabber(CAMERA_NUM);
        int posX,posX2;
        try {
            grabber.start();
            Thread.sleep(1000);
            IplImage img, imTemp;
            IplImage detectThrs, detHSV;
            while (true) {
                img = grabber.grab();
                if (img != null) {
                    imTemp = img.clone();
                    // show image on window
                    //cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise

                    //System.out.println("wwwwwwwwwwwwwwwwww");
                    detectThrs = colDet.getThresholdImage1(img);
                    detHSV=colDet.getThresholdImage2(img);
                    //System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
                    cvSmooth(detectThrs, detectThrs, CV_MEDIAN, 15);
                    cvSmooth(detHSV, detHSV, CV_MEDIAN, 15);
                    //cvSaveImage(++ii + "GreenThreshold.jpg", imgThreshold);
                    //System.out.println("hghsgsgsg");
                    //detectThrs = this.getThresholdImage1(img);

                    posX = meanDet.getMeanX(detectThrs);
                    CvPoint start = new CvPoint(posX, 0);
                    CvPoint end = new CvPoint(posX, detectThrs.width());
                    if (posX > 0) {
                        cvDrawLine(imTemp, start, end, CvScalar.RED, 4, 8, 0);
                    }


                     posX2 = meanDet.getMeanX(detHSV);
                    CvPoint start2 = new CvPoint(posX2, 0);
                    CvPoint end2 = new CvPoint(posX2, detHSV.width());
                    if (posX2 > 0) {
                        cvDrawLine(imTemp, start2, end2, CvScalar.RED, 4, 8, 0);
                    }
                    
                    pathHSV.showImage(detHSV);
                    pathRGB.showImage(detectThrs);
                    canvas.showImage(img);
                }
                //Thread.sleep(INTERVAL);
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(ColorRedTest.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
            System.out.println("Exception in Color Mean Detector");
            e.printStackTrace();
        }
    }

    /**
     * @return the theta
     */
    public double getTheta() {
        return theta;
    }
}
